package com.whut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.whut.model.Userinfo;
import com.whut.model.Userpermission;
import com.whut.service.PersonnelService;
import com.whut.utils.MD5Encoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/personnel")
public class PersonnelController {
    /*
    * @@Author 彭西明
    * @@Date 2019-05-22
    *
    * 人员管理模块
    * */
    @Resource
    private PersonnelService personnelService;

    @ResponseBody
    @PostMapping("/addUser")
    public Object addUser(Userinfo user){
//       System.out.println(user);
        //需要判断手机号码唯一
        Userinfo u = personnelService.getUserByUserPhone(user.getUserPhone());
        if (u!=null) {
            return "duplicate";
        }
        user.setPassword(MD5Encoder.getMD5("666666"));//初始密码
        return  personnelService.addUser(user);
    }

    @ResponseBody
    @PostMapping("/Register")
    public Object Register(Userinfo user){
        Userinfo u = personnelService.getUserByUserPhone(user.getUserPhone());
        if (u!=null) {
            return "duplicate";
        }
        System.out.println(user.getPassword());
        user.setPassword(MD5Encoder.getMD5(user.getPassword()));//初始密码
        return  personnelService.addUser(user);
    }

    @ResponseBody
    @PostMapping("/getUserByPage")
        public Object getUserByPage(String searchParam,int pageNum,int pageSize){
        if (searchParam.equals("") || searchParam== null){
            return personnelService.getUserByPage(pageNum,pageSize);
        }else{
            return personnelService.getUserByUserName(searchParam);
        }
    }

    @ResponseBody
    @PostMapping("/deleteUserByUserId")
    public String deleteUserByUserId(int userId){
        return personnelService.deleteUserByUserId(userId);
    }

    @ResponseBody
    @PostMapping("/resetPasswordByUserId")
    public String resetPasswordByUserId(int userId){
        personnelService.updatePasswordByUserId(userId,MD5Encoder.getMD5("666666"));
        return "ok";
    }

    @ResponseBody
    @PostMapping("/updateUserinfo")
    public String updateUserinfo(Userinfo user){

        personnelService.updateUserinfo(user);
        return "ok";
    }

    @ResponseBody
    @PostMapping("/resetUserPassword")
    public String resetUserPassword(Userinfo user){
        user.setPassword(MD5Encoder.getMD5(user.getPassword()));
        personnelService.resetUserPassword(user);
        return "ok";
    }

    @ResponseBody
    @PostMapping("/getPermission")
    public Object getPermission(){
        return personnelService.getPermission();
    }
    @ResponseBody
    @PostMapping("/getPersonalPermission")
    public Object getPersonalPermission(int userId){
        return personnelService.getPersonalPermission(userId);
    }

    @ResponseBody
    @PostMapping("/updatePermission")
    public String updatePermissin(int userId,String permission){
        if(permission==null){
            permission = " ";
        }
        permission = permission.replace(',',' ');
        personnelService.updatePermission(userId,permission);
        return "ok";
    }

    @ResponseBody
    @PostMapping("/updatePermissionByPosition")
    public String updatePermissionByPosition(String position,String permission,String newPosition){
        personnelService.updatePermissionByPosition(position,permission,newPosition);
        return "OK";
    }

    @ResponseBody
    @PostMapping("/getAllPosition")
    public List<Object> getAllPosition(){
        return personnelService.getAllPosition();
    }

    @ResponseBody
    @PostMapping("/getAllModular")
    public List<Object> getAllModular(){
        return personnelService.getAllModular();
    }

    @ResponseBody
    @PostMapping("/getPermissionByModular")
    public List<Object> getPermissionByModular(String modular){
        return personnelService.getPermissionByModular(modular);
    }

    @ResponseBody
    @PostMapping("/setPermissionByPosition")
    public String setPermissionByPosition(Userpermission userpermission){
        return personnelService.setPermissionByPosition(userpermission);
    }

    @ResponseBody
    @PostMapping("/getPermissionByPermissionNum")
    public Userpermission getPermissionByPermissionNum(Integer permissionNum){
        return personnelService.getPermissionByPermissionNum(permissionNum);
    }

    @ResponseBody
    @PostMapping("/deleteByPosition")
    public String deleteByPosition(String position){
        return personnelService.deleteByPosition(position);
    }

    @ResponseBody
    @PostMapping("/deletePosition")
    public String deletePosition(String position){
        return personnelService.deletePosition(position);
    }

    @ResponseBody
    @PostMapping("/addPosition")
    public String addPosition(String position){
        return personnelService.addPosition(position);
    }

    @ResponseBody
    @PostMapping("/getPermissionByPosition")
    public List<Object> getPermissionByPosition(String position){
        return personnelService.getPermissionByPosition(position);
    }

    @ResponseBody
    @PostMapping("/getAllChildSystem")
    public List<Object> getAllChildSystem(){
        return personnelService.getAllChildSystem();
    }

    @ResponseBody
    @PostMapping("/getModularByChildSystem")
    public List<Object> getModularByChildSystem(String childSystem){
        return personnelService.getModularByChildSystem(childSystem);
    }

    @ResponseBody
    @PostMapping("/getPermissionByUserId")
    public String getPermissionByUserId(String userId){
        return personnelService.getPermissionByUserId(userId);
    }
}
